package cn.stylefeng.guns.czlm.controller;

 import cn.stylefeng.guns.core.common.annotion.BussinessLog;
 import cn.stylefeng.guns.core.common.annotion.Permission;
 import cn.stylefeng.guns.core.common.constant.Const;
 import cn.stylefeng.guns.core.common.constant.dictmap.DeleteDict;
 import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
 import cn.stylefeng.guns.czlm.entity.TbAttribute;
 import cn.stylefeng.guns.czlm.entity.TbAttributeValue;
 import cn.stylefeng.guns.czlm.mapper.TbAttributeValueMapper;
 import cn.stylefeng.guns.czlm.service.TbAttributeService;
 import cn.stylefeng.guns.czlm.service.TbAttributeValueService;
 import cn.stylefeng.roses.core.base.controller.BaseController;
 import cn.stylefeng.roses.core.reqres.response.ResponseData;
 import cn.stylefeng.roses.core.util.ToolUtil;
 import cn.stylefeng.roses.kernel.model.exception.ServiceException;
 import com.alibaba.fastjson.JSON;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.*;

 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;

 /**
 * 商品规格控制器
 *
 * @author zzh
 * @Date 2019-4-29
 */
@Controller
@RequestMapping("/tbattribute")
public class TbAttributeController extends BaseController {
    private static String PREFIX = "/modular/system/tbattribute";

    @Autowired
    private TbAttributeService tbAttributeService;

    @Autowired
    private TbAttributeValueService tbAttributeValueService;

    private  int roleId;//接受id值

    /**
     * 跳转到商品列表页面
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:30 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/tbattribute.html";
    }

    /**
     *查询所有
     */
    @RequestMapping(value = "/tbalist")
    @ResponseBody
    public List list() {

          List<TbAttribute>  list= tbAttributeService.findTbAttribute();

          List<Object>  listnew= new ArrayList<Object>();

           for (TbAttribute  t : list){
               Map<String,Object>  map=new HashMap<String,Object>();
               map.put("id",t.getId());
               map.put("name",t.getName());
               String  value="";
                 for ( TbAttributeValue l :   t.getTbAttributeValues()){
                       value+=l.getValue()+"  ";
                    }
                 map.put("value",value);
                 listnew.add(map);
           }
         //  String   str=JSON.toJSONString(listnew);
           return listnew ;
    }
    /**
     *跳转 规格名称
     * @return
     */
    @RequestMapping(value ="/tbattribute_add")
    public  String add(String formOk){
        return PREFIX + "/tbattribute_add.html";
    }
     /**
      * 添加规格
      */
     @RequestMapping(value ="/add")
     @ResponseBody
     public String addtbav(String formName){
         try {
             tbAttributeService.insertTbAttribute(formName);
             tbAttributeValueService.insertDefault();
         }catch (Exception e){
             return "false";
         }
         return "true";
     }
     /**
      * 跳转规格值
      * @return
      */
     @RequestMapping(value ="/tbattribute_edit")
     public  String edit(@RequestParam int id){
         if (ToolUtil.isEmpty(id)) {
             throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
         }
         roleId=id;  //规格id
         return PREFIX + "/tbattribute_edit.html";
     }
     /**
      * 添加规格值
      */
     @RequestMapping(value = "/tbavedit")
     @ResponseBody
     public String  tbavedit(String edit){
        try {
            tbAttributeValueService.insertTbAttributeValue(roleId,edit);
        }catch (Exception e){
            return "false";
        }
         return "true";
     }
     /**
      * 规格删除功能
      * @param attributeId
      * @return
      */
    //@RequestMapping(value = "/remove/{id}/{name}/{value}",method = RequestMethod.POST)@PathVariable
     @RequestMapping(value = "/remove")
     public ResponseData remove(@RequestParam int id){
         if (ToolUtil.isEmpty(id)) {
             throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
         }
        this.tbAttributeService.deleteTbAttributeById(id);
        this.tbAttributeValueService.deleteTbAttributeValueById(id);
      return SUCCESS_TIP;
     }
 }
